package org.bitoy.shimmer.adapter.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.bitoy.shimmer.adapter.controller.dto.DemoDTO;
import org.bitoy.shimmer.adapter.controller.dto.DemoRetrieveDTO;
import org.bitoy.shimmer.common.enums.Gender;
import org.bitoy.shimmer.common.exception.DemoException;
import org.bitoy.shimmer.config.DemoProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/demo")
@Tag(name = "demo controller")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
//@Scope("prototype")
public class DemoController {
    private int num = 0; // controller默认单例，controller实例变量是线程不安全的

    private DemoProperties demoProperties;

    @Operation(summary = "demo检索")
    @GetMapping
    public DemoDTO retrieve(@Valid DemoRetrieveDTO retrieveDTO) {
        DemoDTO dto = new DemoDTO();
        dto.setId(retrieveDTO.getId());
        dto.setName(retrieveDTO.getName());
        dto.setAddress("abc");
        if ("ex".equals(retrieveDTO.getName())) throw new DemoException("ex");
        System.out.println(retrieveDTO.getGender().ordinal());
        System.out.println("num = " + (++num));
        return dto;
    }

    @GetMapping("/{id}")
    public DemoDTO get(@PathVariable("id") Long id) {
        DemoDTO dto = new DemoDTO();
        dto.setId(id);
        dto.setGender(Gender.MALE);
        //dto.setName(demoProperties.getName());
        System.out.println("num = " + (++num));
        return dto;
    }

    @PostMapping()
    public void save(@Valid @RequestBody DemoDTO dto) {
        System.out.println(dto.getGender().desc1);
    }
}
